//
//  LineView.m
//  PriceOnline
//
//  Created by Le Anh Tai on 8/29/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "LineView.h"

typedef enum
{
    LineTypeLoi,
    LineTypeLom
}LineType;

@interface LineView()
{
    CGFloat _lineThin;
    LineType _lineType;
}

@end

@implementation LineView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self initialize];
    }
    
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        [self initialize];
    }
    
    return self;
}

- (void)initialize
{
    self.backgroundColor = [UIColor clearColor];
    _lineThin = 0.1;
    _lineType = LineTypeLom;
}

- (void)setFrame:(CGRect)frame
{
    frame.size.height = 2;
    [super setFrame:frame];
}

-(void)drawRect:(CGRect)rect
{
    CGFloat lineWid = _lineThin;
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, lineWid);
    
    CGMutablePathRef path = CGPathCreateMutable();
    
    //line top
    CGPathMoveToPoint(path, nil, 0, self.height/2 - lineWid/2);
    CGPathAddLineToPoint(path, nil, self.width,self.height/2 - lineWid/2);
    if (_lineType == LineTypeLom)
    {
        [[UIColor blackColor] setStroke];
    }
    else
    {
        [[UIColor whiteColor] setStroke];
    }
    
    CGContextAddPath(context, path);
    CGContextDrawPath(context, kCGPathFillStroke);
    
    CGPathRelease(path);
    
    //line bottom
    path = CGPathCreateMutable();
    CGPathMoveToPoint(path, nil, 0, self.height/2 + lineWid/2);
    CGPathAddLineToPoint(path, nil, self.width, self.height/2 + lineWid/2);
    
    if (_lineType == LineTypeLom)
    {
        [[UIColor whiteColor] setStroke];
    }
    else
    {
        [[UIColor blackColor] setStroke];
    }
    CGContextAddPath(context, path);
    CGContextDrawPath(context, kCGPathFillStroke);
    
    CGPathRelease(path);
}
@end
